﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Core.Logging;
using MyStore.PresentationModel.Models.Users;

namespace MyStore.PresentationModel.Controllers
{
    public class UserController : Controller
    {
        public ILogger Logger { get; set; }

        public ActionResult LogIn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogIn(LogInModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
                return View(model);

            if (model.UserName != "admin")
            {
                Logger.WarnFormat("User {0} attempted login but password validation failed", model.UserName);
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
                return View(model);
            }

            System.Web.Security.FormsAuthentication.SetAuthCookie(model.UserName, false);

            if (string.IsNullOrEmpty(returnUrl))
                return RedirectToAction("Index", "Home");

            return new RedirectResult(returnUrl);
        }


        public ActionResult LogOut()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}
